package com.ffzx.commerce.framework.utils;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.util.Pool;
/**
 * 
* @ClassName: CodeGeneratorImpl 
* @Description:  CodeGenerator
* @author 李淼淼  445052471@qq.com
* @date 2016年7月12日 下午2:01:08
 */
public class CodeGeneratorImpl implements CodeGenerator {

	private Logger logger=LoggerFactory.getLogger(getClass());
	
	private Pool<Jedis> jedisPool;

	public void setJedisPool(Pool<Jedis> jedisPool) {
		this.jedisPool = jedisPool;
	}

	public String generate(String prefix) {
		return generate(prefix, "yyyyMMdd");
	}

	public static String format(Number i, int len) {
		return String.format("%0" + len + "d", i);

	}

	public String generate(String prefix, int len) {
		return generate(prefix, "yyyyMMdd", len);
	}

	public String generate(String prefix, String format, int len) {
		String storeType = "seq:" + prefix;
		Jedis jedis=null;
		try {
			jedis = jedisPool.getResource();
			if (jedis.setnx(storeType, 0 + "") > 0) {
				if("TIMESTAMP".equals(format)){
					//jedis.expire(storeType, 1);
				}else{
					jedis.expire(storeType, 3600 * 24);
				}
			}
			Long i = jedis.incr(storeType);
			if ("TIMESTAMP".equals(format)) {
				String seqCode = format(i, len);
				if (seqCode.length() > len) {
					jedis.del(storeType);
					return generate(prefix, format, len);
				}
			}
			List<String> time = jedis.time();
			long t = Long.parseLong(time.get(0) + "000");
			long m = Long.parseLong(time.get(1));
			t = t + m / 1000;
			if("TIMESTAMP".equals(format)){
				String code=String.format("%d%0" + len + "d", t, i);
				code=code.substring(1);
				code=String.format("%s%s", prefix,code);
				return code;
			}
			return String.format("%s%s%0" + len + "d", prefix, DateUtils.format(format, new Date(t)), i);
			//jedis.close();
		} catch (Exception e) {
			logger.debug("generate code fail");
			throw new RuntimeException(e);
		}finally {
			if(jedis!=null){
				jedis.close();
			}
		}
		
	}

	public String generate(String prefix, String format) {
		return generate(prefix, format, 8);
	}
	
	public static void main(String[] args) throws IOException {
		String host="127.0.0.1";
		int port=6379;
		JedisPool jedisPool = new JedisPool(host, port);
		CodeGeneratorImpl codeGenerator=new CodeGeneratorImpl();
		codeGenerator.setJedisPool(jedisPool);
		String prefix="O2O_";
		//prefix=prefix;
		List<String> codes=new ArrayList<>();
		Set<String> codeMaps=new HashSet<>();
		long t=System.currentTimeMillis();
		for(int i=0;i<1;i++){
			String code=codeGenerator.generate(prefix,"TIMESTAMP",3);
			System.out.println(code);
			codes.add(code);
			codeMaps.add(code);
		}
		t=System.currentTimeMillis()-t;
		
		System.out.println("total "+codes.size());
		System.out.println("unique total "+codeMaps.size());
		System.out.println("take "+t+" ms");
		FileWriter writer=new FileWriter("d:/code.txt");
		for (String key : codes) {
			writer.write(key+"\n");
		}
		writer.flush();
		writer.close();
	
	}
}
